<template>
  <!-- 4. 底部组件 -->
  <div class="my-footer">
    <!-- 9. 全选功能 -->
    <div class="custom-control custom-checkbox">
      <input
        type="checkbox"
        class="custom-control-input"
        id="footerCheck"
        v-model="isAll"
      />
      <label class="custom-control-label" for="footerCheck">全选</label>
    </div>
    <!-- 合计 -->
    <div>
      <span>合计:</span>
      <span class="price">¥ {{ allPrice }}</span>
    </div>
    <!-- 按钮 -->
    <!-- 10. 总数量 footer--count -->
    <button type="button" class="footer-btn btn btn-primary">
      结算 ({{ allCount }})
    </button>
  </div>
</template>

<script>
export default {
  //  9. 全选功能
  props: ["Arr"],
  computed: {
    isAll: {
      set(val) {
        this.Arr.forEach((obj) => (obj.goods_state = val));
      },
      get() {
        return this.Arr.every((obj) => obj.goods_state);
      },
    },

    // 10. 底部总数量
    allCount() {
      // 先筛选出来, 再累计
      return this.Arr.filter((obj) => obj.goods_state).reduce(
        (sum, obj) => (sum += obj.goods_count),
        0
      );
    },

    // 11. 总价
    allPrice() {
      return this.Arr.filter((obj) => obj.goods_state).reduce(
        (sum, obj) => (sum += obj.goods_count * obj.goods_price),
        0
      );
    },
  },
};
</script>

<style lang="less" scoped>
.my-footer {
  position: fixed;
  z-index: 2;
  bottom: 0;
  width: 100%;
  height: 50px;
  border-top: 1px solid #ccc;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 10px;
  background: #fff;

  .price {
    color: red;
    font-weight: bold;
    font-size: 15px;
  }
  .footer-btn {
    min-width: 80px;
    height: 30px;
    line-height: 30px;
    border-radius: 25px;
    padding: 0;
  }
}
</style>